package cn.keepme.redis;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import cn.keepme.entity.HackerVisitLog;
import cn.keepme.entity.vo.User;

public class TestLog {
	
//	static RedisConnectionFactory connectionFactory = ApplicationConfig.redisConnectionFactory();
	
	public static void main(String[] args) {
		System.out.println(">>>> 开始");
//		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml");
//		RedisTemplate redisTemplate = (RedisTemplate) ac.getBean("redisTemplate");
		RedisConnectionFactory connectionFactory = (RedisConnectionFactory) ac.getBean("connectionFactory");
		
//    	testJacksonSerialiable(connectionFactory);
//		testJackson(connectionFactory);
		
		RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();
		redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<HackerVisitLog>(HackerVisitLog.class));  
	    redis.afterPropertiesSet();
		
		HackerVisitLog log = new HackerVisitLog();
		log.setId(1);
        log.setType(606);
        log.setApp_id("2205");
        log.setCode_id("1394001");
        log.setImsi("460023416515866");
        log.setImei("863165029634638");
        log.setSerial_sys("123456789011");
        log.setSerial("12345678901122051394001");
        log.setResultcode("100");
        log.setCreate_time(Timestamp.valueOf("2015-07-17 11:36:20"));
        
		setJacksonSerHackerVisitLog(redis,log);
    }
	
	/**
	 * 序列化
	 * @param log
	 */
	public static void setJacksonSerHackerVisitLog(RedisTemplate<String, Object> redis,HackerVisitLog log) {
	    ValueOperations<String, Object> ops = redis.opsForValue();  
	    String key1 = "sn:"+log.getSerial();
	    ops.set(key1,log); 
	}
	
	public void getJacksonSerHackerVisitLog(RedisTemplate<String, Object> redis, HackerVisitLog log) {
	    ValueOperations<String, Object> ops = redis.opsForValue();  
	    String key = "sn:"+log.getSerial();
        HackerVisitLog hackerVisitLog = new HackerVisitLog();
        hackerVisitLog = (HackerVisitLog)ops.get(key); 
        
	    System.out.println(hackerVisitLog.getId()+" - "+hackerVisitLog.getSerial());
	}
	
	
	
	public static void testJackson(RedisConnectionFactory connectionFactory) {
	    RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();  
	    redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<HackerVisitLog>(HackerVisitLog.class));  
	    redis.afterPropertiesSet();  
	  
	    
	    ValueOperations<String, Object> ops = redis.opsForValue();  
	    
	    HackerVisitLog log = new HackerVisitLog();
		log.setId(1);
        log.setType(606);
        log.setApp_id("2205");
        log.setCode_id("1394001");
        log.setImsi("460023416515866");
        log.setImei("863165029634638");
        log.setSerial_sys("123456789011");
        log.setSerial("12345678901122051394001");
        log.setResultcode("100");
        log.setCreate_time(Timestamp.valueOf("2015-07-17 11:36:20"));
        
        HackerVisitLog log11 = null;
	    String key1 = "sn:"+log.getSerial();
	  
	    long begin = System.currentTimeMillis();  
	    for (int i = 0; i < 100; i++) {  
	        ops.set(key1,log);  
	        log11 = (HackerVisitLog)ops.get(key1);  
	    }  
	    long time = System.currentTimeMillis() - begin;  
	  
	    System.out.println(log11.getId()+" - "+log11.getSerial());
	    System.out.println("json time:"+time);  
	   
	} 
	
	
	
	
	public static void testJacksonSerialiable(RedisConnectionFactory connectionFactory) {
	    RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();  
	    redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<User>(User.class));  
	    redis.afterPropertiesSet();  
	  
	    ValueOperations<String, Object> ops = redis.opsForValue();  
	  
	    User user1 = new User();  
	    user1.setUserName("user1");  
	    user1.setAge(20);  
	      
	    User user11 = null;  
	    String key1 = "json/user2";  
	  
	    long begin = System.currentTimeMillis();  
	    for (int i = 0; i < 100; i++) {  
	        ops.set(key1,user1);  
	        user11 = (User)ops.get(key1);  
	    }  
	    long time = System.currentTimeMillis() - begin;  
	  
	    System.out.println(user11.getUserName()+" - "+user11.getAge());
	    System.out.println("json time:"+time);  
	   
	} 
	
	
	
	
	
	/** 
     * 批量向redis中插入H码:key(tableName:hcode) value(pcode) 
     * 如果键已存在则返回false,不更新,防止覆盖。使用pipeline批处理方式(不关注返回值) 
     *  @param list  一个map代表一行记录,2个key:hcode & pcode。 
     *  @param tableName redis中key的值为tableName:hcode  对应value值为pcode。 
     *  @return 
     */  
    public boolean addHcode(final StringRedisTemplate redisTemplate) {
    	HackerVisitLog log = new HackerVisitLog();
		log.setId(1);
        log.setType(606);
        log.setApp_id("2205");
        log.setCode_id("1394001");
        log.setImsi("460023416515866");
        log.setImei("863165029634638");
        log.setSerial_sys("123456789011");
        log.setSerial("12345678901122051394001");
        log.setResultcode("100");
        log.setCreate_time(Timestamp.valueOf("2015-07-17 11:36:20"));
        Map<String, Object> m = new java.util.HashMap<String, Object>();
        m.put("hcode", "log1");
        m.put("pcode", log);
        
        final List<Map<String, Object>> list = new ArrayList();
        list.add(0, m);
		final String tableName = "T";
	
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)  throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                for (Map<String, Object> map : list) {  
                    byte[] key  = serializer.serialize(tableName+":"+map.get("hcode").toString());  
                    byte[] name = serializer.serialize(map.get("pcode").toString());  
                    connection.setNX(key, name);  
                }  
                return true;  
            }  
        }, false, true); 
		System.out.println("result= "+ result);
        return result;  
    }
	
}
